package com.ruoyi.service.controller;

import java.util.LinkedHashMap;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.RemoteCheShopService;
import com.ruoyi.domain.CheShop;
import com.ruoyi.service.service.ICheServiceSortService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.service.domain.CheServiceitems;
import com.ruoyi.service.service.ICheServiceitemsService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 服务管理Controller
 * 
 * @author ruoyi
 * @date 2024-06-05
 */
@RestController
@CrossOrigin
@RequestMapping("/serviceitems")
public class CheServiceitemsController extends BaseController
{
    @Autowired
    private ICheServiceitemsService cheServiceitemsService;
    @Autowired
    private ICheServiceSortService cheServiceSortService;
    @Autowired
    private RemoteCheShopService remoteCheShopService;

    /**
     * 查询服务管理列表
     */
    @RequiresPermissions("service:serviceitems:list")
    @GetMapping("/list")
    public TableDataInfo list(CheServiceitems cheServiceitems)
    {
        startPage();
        List<CheServiceitems> list = cheServiceitemsService.selectCheServiceitemsList(cheServiceitems);
        for (CheServiceitems cheServiceitems1 : list){
            AjaxResult info = remoteCheShopService.getInfo(cheServiceitems1.getShopId());
            LinkedHashMap data = (LinkedHashMap) info.get("data");
            CheShop cheShop = BeanUtil.mapToBean(data, CheShop.class, true);
            cheServiceitems1.setServiceSortName(cheServiceSortService.selectCheServiceSortById(cheServiceitems1.getServiceSortId()).getName());
            cheServiceitems1.setShopName(cheShop.getShopName());
        }
        return getDataTable(list);
    }

    /**
     * 导出服务管理列表
     */
    @RequiresPermissions("service:serviceitems:export")
    @Log(title = "服务管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CheServiceitems cheServiceitems)
    {
        List<CheServiceitems> list = cheServiceitemsService.selectCheServiceitemsList(cheServiceitems);
        ExcelUtil<CheServiceitems> util = new ExcelUtil<CheServiceitems>(CheServiceitems.class);
        util.exportExcel(response, list, "服务管理数据");
    }

    /**
     * 获取服务管理详细信息
     */
    @RequiresPermissions("service:serviceitems:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(cheServiceitemsService.selectCheServiceitemsById(id));
    }

    /**
     * 新增服务管理
     */
    @RequiresPermissions("service:serviceitems:add")
    @Log(title = "服务管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CheServiceitems cheServiceitems)
    {
        System.out.println(cheServiceitems);
        return toAjax(cheServiceitemsService.insertCheServiceitems(cheServiceitems));
    }

    /**
     * 修改服务管理
     */
    @RequiresPermissions("service:serviceitems:edit")
    @Log(title = "服务管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CheServiceitems cheServiceitems)
    {
        return toAjax(cheServiceitemsService.updateCheServiceitems(cheServiceitems));
    }

    /**
     * 删除服务管理
     */
    @RequiresPermissions("service:serviceitems:remove")
    @Log(title = "服务管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        int i = 0;
        for (Long id : ids){
            CheServiceitems cheServiceitems = cheServiceitemsService.selectCheServiceitemsById(id);
            cheServiceitems.setDeleted(1L);
            i = cheServiceitemsService.updateCheServiceitems(cheServiceitems);
        }
        return toAjax(i);
    }


    /**
     * 根据shopId获取服务管理详细信息
     */
//   @RequiresPermissions("service:getName:getName")
    @GetMapping(value = "/getName/{shopId}")
    public TableDataInfo getServiceNa(@PathVariable("shopId") Long shopId)
    {
        List<CheServiceitems> list = cheServiceitemsService.selectCheServiceitemsByShopId(shopId);
        return getDataTable(list);
    }
}
